/**
 * @author [sunshinebooming]
 * @email [sunshinebooming@gmail.com]
 * @create date 2020-11-12 20:13:48
 * @modify date 2020-11-12 20:13:48
 * @desc [lwip linux simulator]
 */
#include <unistd.h>

#include "app_api.h"
#include "tcpip_adapter.h"

static struct netif* netif;
static pthread_t thread;

int main(void)
{
	tcpip_adapter_init();

	/* init linux netif */
	netif = (struct netif *)malloc(sizeof(struct netif));
	if (ERR_OK != linux_netif_init(&netif)) {
		printf("linux netif init error\n");
		goto _exit;
	}

	/* create thread to recv packets */
	if(ERR_OK != create_packet_capture_thread(&thread)) {
		goto _exit;
	}

#if SIM_DHCP_ENABLE
	/* wait for dhcp success */
	while (netif->dhcp->state != DHCP_BOUND) {
		sleep(1);
		printf("wait for dhcp release[%d]...\n", netif->dhcp->state);
	}
	printf("dhcp success\n");
	printf("IP  : %u.%u.%u.%u\n", netif->ip_addr.addr & 0xff, (netif->ip_addr.addr >> 8) & 0xff, \
			(netif->ip_addr.addr >> 16) & 0xff, (netif->ip_addr.addr >> 24) & 0xff);
	printf("GW  : %u.%u.%u.%u\n", netif->gw.addr & 0xff, (netif->gw.addr >> 8) & 0xff, \
			(netif->gw.addr >> 16) & 0xff, (netif->gw.addr >> 24) & 0xff);
	printf("MASK: %u.%u.%u.%u\n", netif->netmask.addr & 0xff, (netif->netmask.addr >> 8) & 0xff, \
			(netif->netmask.addr >> 16) & 0xff, (netif->netmask.addr >> 24) & 0xff);
#endif /* SIM_DHCP_ENABLE */

#if SIM_TCP_ECHO_SERVER
	if (tcp_echo_server_init() != ERR_OK) {
		printf("create echo server error\n");
		goto _exit;
	}
#endif /* SIM_TCP_ECHO_SERVER */

#if SIM_UDP_ECHO_SERVER
	if (udp_echo_server_init() != ERR_OK) {
		printf("create udp echo server failed\n");
		goto _exit;
	}
#endif /* SIM_UDP_ECHO_SERVER */

	pthread_join(thread, NULL);

_exit:
	linux_netif_deinit();
	return 0;
}
